﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using ZhengMeiJiSettingUI.ModelEntity;
namespace ZhengMeiJiSettingUI.Data
{
    public class MaterialTypeServer
    {
     //   private IHttpClientFactory _httpClientFactory { get; set; }
        public MaterialTypeServer()
        {
           // _httpClientFactory = httpClientFactory;
        }
        public async Task<IEnumerable<MaterialTypeEntity>> GetMaterialType()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/MaterialType");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<MaterialTypeEntity>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }
    


        }

        public async Task<bool> ADD(MaterialTypeEntity Value)
        {

            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/MaterialType");
                    request.Headers.Add("Accept", "text / plain");
                    request.Content = new StringContent(JsonSerializer.Serialize<MaterialTypeEntity>(Value), Encoding.UTF8, "application/json");

                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <bool>(responseStream);
                            return result;

                        }
                        else
                        {
                            return false;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return false;
                    }
                }
            }


        }


        public async Task<bool> Delete(int id)
        {
            //  https://localhost:7001/api/BusinessDepartment/13
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Delete,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/MaterialType/{id}");
                    request.Headers.Add("Accept", "text / plain");
                    //request.Content = new StringContent(JsonSerializer.Serialize<BusinessDepartmentEntitycs>(businessDepartmentEntitycs), Encoding.UTF8, "application/json");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <bool>(responseStream);
                            return result;
                        }
                        else
                        {
                            return false;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return false;
                    }
                }
            }
  

        }

        public async Task<bool> UpData(MaterialTypeEntity Value)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Put,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/MaterialType");
                    request.Headers.Add("Accept", "text / plain");
                    request.Content = new StringContent(JsonSerializer.Serialize<MaterialTypeEntity>(Value), Encoding.UTF8, "application/json");

                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {

                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <bool>(responseStream);
                            return result;
                        }
                        else
                        {
                            return false;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return false;
                    }

                }
            }
      
        }
    }
}
